import config from './request.js';
// import store from '@/store/index.js';
import {
	get,
	clear,
	del
} from './db.js'
 
// 超时时间
const timeout = 5 * 1000;

// 接口基础服务地址
const baseUrl = config.apiUrl;

const url = ''

const switch1 = true;

//接口对象
const api = {};

/**
 * 接口方法定义
 * name: 方法名，$api.方法名 进行接口请求
 * desc：接口解释
 * url：接口路径
 * usedLoading：是否使用loading加载提示框，默认true
 * 
 */

const apiList = [

	{
		desc: '登录',
		name: 'doLogin',
		url: 'doLogin',
		usedLoading: true
	},
	{
		desc: '隐藏',
		name: 'showPhoto',
		url: 'api/user/showPhoto',
		usedLoading: true,
		method:'get',
	},
	{
		desc: '注册',
		name: 'registered',
		url: 'api/user/registered',
		usedLoading: true
	},
	{
		
		desc: '验证微信code',
		name: 'verifyCode',
		url: 'api/verifyCode',
		usedLoading: true
	},
	{
		method:'get',
		desc: '查询自己审批任务',
		name: 'selectTask',
		url: 'activiti/node/selectTask',
		usedLoading: true
	},
	{
		
		desc: '完成任务接口',
		name: 'complete',
		url: 'activiti/node/complete',
		usedLoading: true
	},
	{
		
		desc: '上传附件',
		name: 'upload',
		url: 'api/common/attach/upload',
		usedLoading: true
	},
	{
		method:'get',
		desc: '查询用户',
		name: 'query',
		url: 'api/user/query',
		usedLoading: true
	},
	{
		
		desc: '学生申请',
		name: 'student',
		url: 'activities/apply/student',
		usedLoading: true
	},
	{
		
		desc: '访校申请',
		name: 'outsiders',
		url: 'api/apply/outsiders',
		usedLoading: true
	},
	{
		method:'get',
		desc: '获取申请列表',
		name: 'list2',
		url: 'api/apply/current/list',
		usedLoading: true
	},
	
	{
		method:'get',
		desc: '获取历史申请列表',
		name: 'historyList2',
		url: 'api/apply/history/list',
		usedLoading: true
	},
	{
		method:'get',
		desc: '查看申请详情',
		name: 'detail',
		url: 'api/apply/current/detail',
		usedLoading: true
	},
	{
		method:'get',
		desc: '查询学生审查列表',
		name: 'list1',
		url: 'activities/censor/current/list',
		usedLoading: true
	},
	{
		method:'get',
		desc: '获取校门列表',
		name: 'university',
		url: 'api/guard/university/gates',
		usedLoading: true
	},
	{
		desc: '门卫值班',
		name: 'universityDuty',
		url: 'api/guard/university/duty',
		usedLoading: true
	},
	{
		desc: '门卫操作',
		name: 'operation',
		url: 'api/guard/university/operation',
		usedLoading: true
	},
	{
		method:'get',
		desc: '校门历史列表',
		name: 'getOperationhistory',
		url: 'api/guard/operation/history',
		usedLoading: true
	},
	{
		desc: '添加随行人员',
		name: 'getAttached',
		url: 'activities/apply/attached',
		usedLoading: true
	},
	{
		method:'get',
		desc: '门卫扫码',
		name: 'scanQRCode',
		url: 'api/guard/scanQRCode',
		usedLoading: true
	},
	{
		method:'get',
		desc: '获取学院/班级',
		name: 'list',
		url: 'api/dept/list',
		usedLoading: true
	},
	{
		method:'get',
		desc: '获取审批列表',
		name: 'currentList',
		url: 'activities/audit/current/list',
		usedLoading: true
	}, 
	{
		method:'get',
		desc: '获取审历史批列表',
		name: 'historyList',
		url: 'activities/audit/history/list',
		usedLoading: true
	},
	{
		desc: '审批操作',
		name: 'vetting',
		url: 'activities/audit/current/vetting',
		usedLoading: true
	},
	{
		method:'get',
		desc: '历史审查',
		name: 'getHistoryList',
		url: 'activities/censor/history/list',
		usedLoading: true
	},
	{
		method:'get',
		desc: '获取校门列表',
		name: 'getloadData',
		url: 'api/guard/loadData',
		usedLoading: true
	},
	{
		method:'get',
		desc: '获取用户信息',
		name: 'selfinfo',
		url: 'api/user/selfinfo',
		usedLoading: true
	},
	{
		method:'get',
		desc: '获取微信H5的API授权',
		name: 'signature',
		url: 'api/signature',
		usedLoading: true
	}
];



/**
 * 接口遍历
 */
// {
// 	'Access-Control-Allow-Origin':'*',
// 	'Access-Control-Allow-Headers':'*',
// 	'Access-Control-Allow-Method':'Content-Type'
// }
apiList.forEach(item => {
	api[item.name] = function(params) {
		let {
			data,
			header,
			dataType
		} = params || {};

		return new Promise((resolve, reject) => {
			try {
				if (false !== item.usedLoading) {
					uni.showLoading({
						title: 'loading...'
					});
				}
				let header = {
					"Authorization": get('authorization') || ''
				}
				let url = baseUrl + item.url
				if(item.name == 'detail'){
					url = baseUrl + item.url +'/'+data.applyId
				}
				if(item.name == 'list'){
					// url = baseUrl + item.url +'?parentId='+data.parentId
				}
				// if(!get('authorization')){
				// 	uni.redirectTo({
				// 		url:'/pages/authorization/index.vue'
				// 	})
				// }
				
				
				console.log(url);
				uni.request({
					url: url,
					method: item.method || "POST",
					data: data,
					header: header,
					dataType: dataType || "json",
					timeout: timeout,
					success: (res) => {
						if(res.data.code == 0){
							resolve(parseSuccessResponseData(res));
						}else {
							reject(parseFailResponseData(res));
						}
						
					},
					fail: (e) => {
						uni.hideLoading();
						// console.error("接口请求错误：" + item.url, e);
						reject(parseFailResponseData(e));
					},
					complete: (e) => {
						uni.hideLoading()
					}
				});
			} catch (e) {
				reject({
					message: "请求错误",
				});
				showToast("请求错误");
			}
		});
	}
});



/**
 * 转换返回的结果
 * @param {Object} res
 */
function parseSuccessResponseData(res) {
	return res.data || res.result;
}
 
/**
 * http状态错误时封装返回
 * @param {Object} res
 */
function parseFailResponseData(e) {
	uni.hideLoading();
	// 如果时token过期就需要重新验证token接口，最后再请求刚过期的接口
	if(e.data.code == "401"){
		uni.showToast({
			title:e.data.message,
			icon:'none'
		})
		del('info')
		del('authorization')
		
		setTimeout(res=>{
			uni.reLaunch({
				url:'/pages/authorization/index'
			})
		},1500)
		
		
		
	}
	return e;
}

/**
 * 显示提示
 * @param {Object} message
 */
function showToast(message) {
	uni.showToast({
		title: message,
		duration: 1500,
		icon: 'none',
	});
}



export default api
